package com.tsx.wiki.controller;

import com.tsx.wiki.dto.EbookDTO;
import com.tsx.wiki.dto.EbookPageDTO;
import com.tsx.wiki.pojo.Ebook;
import com.tsx.wiki.result.PageResult;
import com.tsx.wiki.result.Result;
import com.tsx.wiki.service.EbookService;
import com.tsx.wiki.vo.EbookQueryVo;
import com.tsx.wiki.vo.EbookVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/ebook")
@Slf4j
public class EbookController {
    @Resource
    private EbookService ebookService;

    @GetMapping("/page")
    public Result<PageResult<EbookVo>> page(@Valid EbookPageDTO ebookPageDTO) {
        PageResult<EbookVo> pageResult = ebookService.page(ebookPageDTO);
        log.info("{}", pageResult);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result<List<Ebook>> list(EbookDTO ebookDTO) {
        List<Ebook> ebooks = ebookService.list(ebookDTO);
        return Result.success(ebooks);
    }

    @GetMapping("/{id}")
    public Result<EbookQueryVo> queryById(@PathVariable Long id) {
        EbookQueryVo ebook = ebookService.selectById(id);
        return Result.success(ebook);
    }

    @PutMapping()
    public Result<Object> updateById(@Valid @RequestBody EbookDTO ebookDTO) {
        ebookService.update(ebookDTO);
        return Result.success();
    }

    @PostMapping()
    public Result<Object> save(@Valid @RequestBody EbookDTO ebookDTO) {
        ebookService.save(ebookDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Object> deleteById(@PathVariable Long id) {
        ebookService.deleteById(id);
        return Result.success();
    }
}
